﻿/*
	Copyright © Bogdan Kobets 2011
	http://ElasticLogic.com
*/

using System.Collections.Generic;
using ElasticLogic.TagsMarker.Concepts;

namespace ElasticLogic.TagsMarker
{

	/// <summary>
	/// Use as return in extension method for type E for convenient manipulate with object tags and categories
	/// </summary>
	/// <typeparam name="E">object (class)</typeparam>
	public class TagsAdapter<E>
		where E : class
	{
		private E entity;

		public TagsAdapter(E entity)
		{
			this.entity = entity;
		}

		public bool IsContainsTags
		{
			get { return Marker<E>.IsEntityContainsAnyTag(this.entity); }
		}

		public List<Tag<E>> Tags
		{
			get { return Marker<E>.GetEntityTags(this.entity); }
		}

		public bool ContainsTag(Tag<E> tag)
		{
			return Marker<E>.IsEntityContainsTag(this.entity, tag);
		}

		/// <summary>
		/// Add new tag to Marker (if not exist) and assign it to current entity
		/// </summary>
		public bool AddTag(string caption)
		{
			if (!Marker<E>.Tags.Contains(caption))
			{
				if (!Marker<E>.Tags.Add(caption))
					return false;
			}

			Marker<E>.Tags[caption].IncludeInEntity(this.entity);
			return true;
		}

		/// <summary>
		/// Assign existing tag with such caption to current entity
		/// </summary>
		public bool IncludeTag(string caption)
		{
			if (Marker<E>.Tags.Contains(caption))
			{
				Marker<E>.Tags[caption].IncludeInEntity(this.entity);
				return true;
			}
			else
				return false;
		}

		/// <summary>
		/// Dispose existing tag with such caption from current entity (but not delete tag from Marker)
		/// </summary>
		public bool ExcludeTag(string caption)
		{
			if (Marker<E>.Tags.Contains(caption))
				return Marker<E>.Tags[caption].ExcludeFromEntity(this.entity);
			else
				return false;
		}

		public bool ClearTags()
		{
			return Marker<E>.ClearEntityTags(this.entity);
		}

		/// <summary>
		/// Add current entity to Marker
		/// </summary>
		public void Add()
		{
			Marker<E>.Entities.Add(this.entity);
		}

		/// <summary>
		/// Remove current entity from Marker
		/// </summary>
		public bool Remove()
		{
			return Marker<E>.Entities.Remove(this.entity);
		}

	}

}
